package com.wteam.dragon.shop.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.mapper.*;
import com.wteam.dragon.shop.pojo.bean.FlashPromotion;
import com.wteam.dragon.shop.pojo.bean.FlashPromotionProduct;
import com.wteam.dragon.shop.pojo.bean.FlashPromotionSession;
import com.wteam.dragon.shop.pojo.dto.FlashPromotionProductDto;
import com.wteam.dragon.shop.pojo.dto.FlashPromotionSessionDto;
import com.wteam.dragon.shop.service.FlashPromotionService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2021/3/24 14:48
 */
@Service
@RequiredArgsConstructor
public class FlashPromotionServiceImpl implements FlashPromotionService {
    private final FlashPromotionMapper flashPromotionMapper;
    private final FlashPromotionSessionMapper flashPromotionSessionMapper;
    private final FlashPromotionSessionDtoMapper flashPromotionSessionDtoMapper;
    private final FlashPromotionProductMapper flashPromotionProductMapper;
    private final FlashPromotionProductDtoMapper flashPromotionProductDtoMapper;
    private final ProductMapper productMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addFlashPromotion(FlashPromotion flashPromotion) {
        flashPromotionMapper.insert(flashPromotion);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delFlashPromotion(List<Long> ids) {
        flashPromotionMapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editFlashPromotion(FlashPromotion flashPromotion) {
        flashPromotionMapper.updateById(flashPromotion);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }


    @Override
    public ResultMessage pageFlashPromotion(Page<FlashPromotion> page, String title) {
        QueryWrapper<FlashPromotion> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(title != null, FlashPromotion::getTitle, title);
        Page<FlashPromotion> flashPromotionPage = flashPromotionMapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionPage", flashPromotionPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getFlashPromotion(Long id) {
        FlashPromotion flashPromotion = flashPromotionMapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotion", flashPromotion);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addFlashPromotionSession(FlashPromotionSession flashPromotionSession) {
        flashPromotionSessionMapper.insert(flashPromotionSession);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delFlashPromotionSession(Long id) {
        flashPromotionSessionMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editFlashPromotionSession(FlashPromotionSession flashPromotionSession) {
        flashPromotionSessionMapper.updateById(flashPromotionSession);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage listFlashPromotionSession() {
        QueryWrapper<FlashPromotionSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByAsc(FlashPromotionSession::getStartTime);
        List<FlashPromotionSession> flashPromotionSessionList = flashPromotionSessionMapper.selectList(queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionSessionList", flashPromotionSessionList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage listFlashPromotionSessionById(Long id) {
        QueryWrapper<FlashPromotionSessionDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByAsc(FlashPromotionSessionDto::getStartTime);
        List<FlashPromotionSessionDto> flashPromotionSessionDtoList = flashPromotionSessionDtoMapper.selectList(queryWrapper);
        flashPromotionSessionDtoList.forEach(flashPromotionSessionDto -> {
            QueryWrapper<FlashPromotionProduct> productQueryWrapper = new QueryWrapper<>();
            productQueryWrapper.lambda()
                    .eq(FlashPromotionProduct::getFlashPromotionId, id)
                    .eq(FlashPromotionProduct::getFlashPromotionSessionId, flashPromotionSessionDto.getId());
            flashPromotionSessionDto.setProductCount(flashPromotionProductMapper.selectCount(productQueryWrapper));
        });
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionSessionDtoList", flashPromotionSessionDtoList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getFlashPromotionSession(Long id) {
        FlashPromotionSession flashPromotionSession = flashPromotionSessionMapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionSession", flashPromotionSession);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addFlashPromotionProduct(List<FlashPromotionProduct> list) {
        list.forEach(flashPromotionProduct -> {
            if(flashPromotionProduct.getFlashPromotionId() == null || flashPromotionProduct.getFlashPromotionSessionId() == null || flashPromotionProduct.getProductId() == null){
                throw new BadRequestException("参数有误");
            }
            flashPromotionProductMapper.insert(flashPromotionProduct);
        });
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delFlashPromotionProduct(Long id) {
        flashPromotionProductMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editFlashPromotionProduct(FlashPromotionProduct flashPromotionProduct) {
        flashPromotionProductMapper.updateById(flashPromotionProduct);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage pageFlashPromotionProduct(Page<FlashPromotionProductDto> page, Long flashPromotionSessionId, Long flashPromotionId) {
        QueryWrapper<FlashPromotionProductDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(FlashPromotionProductDto::getFlashPromotionSessionId, flashPromotionSessionId)
                .eq(FlashPromotionProductDto::getFlashPromotionId, flashPromotionId)
                .orderByAsc(FlashPromotionProductDto::getSort);
        Page<FlashPromotionProductDto> flashPromotionProductDtoPage = flashPromotionProductDtoMapper.selectPage(page, queryWrapper);
        flashPromotionProductDtoPage.getRecords().forEach(flashPromotionProductDto -> flashPromotionProductDto.setProduct(productMapper.selectById(flashPromotionProductDto.getProductId())));
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionProductDtoPage", flashPromotionProductDtoPage);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage getFlashPromotionProduct(Long id) {
        FlashPromotionProductDto flashPromotionProductDto = flashPromotionProductDtoMapper.selectById(id);
        flashPromotionProductDto.setProduct(productMapper.selectById(flashPromotionProductDto.getProductId()));
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("flashPromotionProductDto", flashPromotionProductDto);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
